package com.example.skudemo.utils;

import java.util.Random;

/**
 * 随机字符串
 */
public class RandomStringUtil {

    /**
     * 指定位数的字符串
     * @param length
     * @return
     */
    public static String getRandomString(int length) {
        return RandomStringUtil.getString(length);
    }

    /**
     * 指定位数的数字
     * @param len
     * @return
     */
    public static String getStringNumber(int len) {
        int rs = (int) ((Math.random() * 9 + 1) * Math.pow(10, len - 1));
        return String.valueOf(rs);
    }

    private static String getString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            stringBuffer.append(str.charAt(number));
        }
        return stringBuffer.toString();
    }
}
